<?php
mb_internal_encoding('UTF-8');

global $html_entities,$composite,$unicode,$Upper;
$composite=array('aÌ€','aÌ�','aÌ£','aÌ‰','aÌƒ','Ã¢Ì€','Ã¢Ì�','Ã¢Ì£','Ã¢Ì‰','Ã¢Ìƒ','ÄƒÌ�','ÄƒÌ€','ÄƒÌ£','ÄƒÌ‰','ÄƒÌƒ',
    'eÌ€','eÌ�','eÌ£','eÌ‰','eÌƒ','ÃªÌ€','ÃªÌ�','ÃªÌ£','ÃªÌ‰','ÃªÌƒ','iÌ€','iÌ�','iÌ£','iÌ‰','iÌƒ',
    'oÌ€','oÌ�','oÌ£','oÌ‰','oÌƒ','Ã´Ì€','Ã´Ì�','Ã´Ì£','Ã´Ì‰','Ã´Ìƒ','Æ¡Ì€','Æ¡Ì�','Æ¡Ì£','Æ¡Ì‰','Æ¡Ìƒ',
    'uÌ€','uÌ�','uÌ£','uÌ‰','uÌƒ','Æ°Ì€','Æ°Ì�','Æ°Ì£','Æ°Ì‰','Æ°Ìƒ',
    'yÌ€','yÌ�','yÌ£','yÌ‰','yÌƒ',
    'AÌ€','AÌ�','AÌ£','AÌ‰','AÌƒ','Ã‚Ì€','Ã‚Ì�','Ã‚Ì£','Ã‚Ì‰','Ã‚Ìƒ','Ä‚Ì€','Ä‚Ì�','Ä‚Ì£','Ä‚Ì‰','Ä‚Ìƒ',
    'EÌ€','EÌ�','EÌ£','EÌ‰','EÌƒ','ÃŠÌ€','ÃŠÌ�','ÃŠÌ‰','ÃŠÌƒ',
    'IÌ€','IÌ�','IÌ£','IÌ‰','IÌƒ',
    'OÌ€','OÌ�','OÌ£','OÌ‰','OÌƒ','Ã”Ì�','Ã”Ì£','Ã”Ì‰','Ã”Ìƒ','Æ Ì€','Æ Ì�','Æ Ì£','Æ Ì‰','Æ Ìƒ',
    'UÌ€','UÌ�','UÌ£','UÌ‰','UÌƒ','Æ¯Ì€','Æ¯Ì�','Æ¯Ì£','Æ¯Ì‰','Æ¯Ìƒ',
    'YÌ€','YÌ�','YÌ£','YÌ‰','YÌƒ');

$unicode=array('Ã ','Ã¡','áº¡','áº£','Ã£','áº§','áº¥','áº­','áº©','áº«','áº¯','áº±','áº·','áº³','áºµ',
    'Ã¨','Ã©','áº¹','áº»','áº½','á»�','áº¿','á»‡','á»ƒ','á»…','Ã¬','Ã­','á»‹','á»‰','Ä©',
    'Ã²','Ã³','á»�','á»�','Ãµ','á»“','á»‘','á»™','á»•','á»—','á»�','á»›','á»£','á»Ÿ','á»¡',
    'Ã¹','Ãº','á»¥','á»§','Å©','á»«','á»©','á»±','á»­','á»¯',
    'á»³','Ã½','á»µ','á»·','á»¹',
    'Ã€','Ã�','áº ','áº¢','Ãƒ','áº¦','áº¤','áº¬','áº¨','áºª','áº°','áº®','áº ','áº²','áº´',
    'Ãˆ','Ã‰','áº¸','áºº','áº¼','á»€','áº¾','á»‚','á»„',
    'ÃŒ','Ã�','á»Š','á»ˆ','Ä¨',
    'Ã’','Ã“','á»Œ','á»Ž','Ã•','á»�','á»˜','á»”','á»–','á»œ','á»š','á»¢','á»ž','á» ',
    'Ã™','Ãš','á»¤','á»¦','Å¨','á»ª','á»¨','á»°','á»¬','á»®',
    'á»²','Ã�','á»´','á»¶','á»¸');
$Upper=array('A'=>1,'Ã€'=>1,'Ã�'=>1,'áº '=>1,'áº¢'=>1,'Ãƒ'=>1,
    'Ã‚'=>1,'áº¦'=>1,'áº¤'=>1,'áº¬'=>1,'áº¨'=>1,'áºª'=>1,
    'Ä‚'=>1,'áº°'=>1,'áº®'=>1,'áº '=>1,'áº²'=>1,'áº´'=>1,
    'B'=>1,'C'=>1,'D'=>1,'Ä�'=>1,
    'E'=>1,'F'=>1,
    'Ãˆ'=>1,'Ã‰'=>1,'áº¸'=>1,'áºº'=>1,'áº¼'=>1,
    'ÃŠ'=>1,'á»€'=>1,'áº¾'=>1,'á»‚'=>1,'á»„'=>1,
    'G'=>1,'H'=>1,
    'I'=>1,'ÃŒ'=>1,'Ã�'=>1,'á»Š'=>1,'á»ˆ'=>1,'Ä¨'=>1,
    'J'=>1,'K'=>1,'L'=>1,'M'=>1,'N'=>1,
    'O'=>1,
    'Ã’'=>1,'Ã“'=>1,'á»Œ'=>1,'á»Ž'=>1,'Ã•'=>1,
    'Ã”'=>1,
    'L'=>1,
    'á»�'=>1,'á»˜'=>1,'á»”'=>1,'á»–'=>1,
    'Æ '=>1,
    'á»œ'=>1,'á»š'=>1,'á»¢'=>1,'á»ž'=>1,'á» '=>1,
    'P'=>1,'Q'=>1,'U'=>1,'V'=>1,'V'=>1,'Z'=>1,'X'=>1,'T'=>1,'S'=>1,'R'=>1,'W'=>1,
    'Ã™'=>1,'Ãš'=>1,'á»¤'=>1,'á»¦'=>1,'Å¨'=>1,
    'Æ¯'=>1,
    'á»ª'=>1,'á»¨'=>1,'á»°'=>1,'á»¬'=>1,'á»®'=>1,
    'Y'=>1,'á»²'=>1,'Ã�'=>1,'á»´'=>1,'á»¶'=>1,'á»¸'=>1,'D'=>1,'L'=>1,'H'=>1);
$html_entities = array(

    '&Aacute;' => 'Ã�',  '&aacute;' => 'Ã¡',  '&Acirc;' => 'Ã‚',  '&acirc;' => 'Ã¢',  '&acute;' => 'Â´',  '&AElig;' => 'Ã†',  '&aelig;' => 'Ã¦',  '&Agrave;' => 'Ã€',
    '&agrave;' => 'Ã ',  '&alefsym;' => 'â„µ',  '&Alpha;' => 'Î‘',  '&alpha;' => 'Î±',  '&amp;' => '&',  '&and;' => 'âˆ§',  '&ang;' => 'âˆ ',  '&Aring;' => 'Ã…',
    '&aring;' => 'Ã¥',  '&asymp;' => 'â‰ˆ',  '&Atilde;' => 'Ãƒ',  '&atilde;' => 'Ã£',  '&Auml;' => 'Ã„',  '&auml;' => 'Ã¤',  '&bdquo;' => 'â€ž',  '&Beta;' => 'Î’',
    '&beta;' => 'Î²',  '&brvbar;' => 'Â¦',  '&bull;' => 'â€¢',  '&cap;' => 'âˆ©',  '&Ccedil;' => 'Ã‡',  '&ccedil;' => 'Ã§',  '&cedil;' => 'Â¸',  '&cent;' => 'Â¢',
    '&Chi;' => 'Î§',  '&chi;' => 'Ï‡',  '&circ;' => 'Ë†',  '&clubs;' => 'â™£',  '&cong;' => 'â‰…',  '&copy;' => 'Â©',  '&crarr;' => 'â†µ',  '&cup;' => 'âˆª',
    '&curren;' => 'Â¤',  '&dagger;' => 'â€ ',  '&Dagger;' => 'â€¡',  '&darr;' => 'â†“',  '&dArr;' => 'â‡“',  '&deg;' => 'Â°',  '&Delta;' => 'Î”',  '&delta;' => 'Î´',
    '&diams;' => 'â™¦',  '&divide;' => 'Ã·',  '&Eacute;' => 'Ã‰',  '&eacute;' => 'Ã©',  '&Ecirc;' => 'ÃŠ',  '&ecirc;' => 'Ãª',  '&Egrave;' => 'Ãˆ',  '&egrave;' => 'Ã¨',
    '&empty;' => 'âˆ…',  '&emsp;' => 'â€ƒ',  '&ensp;' => 'â€‚',  '&Epsilon;' => 'Î•',  '&epsilon;' => 'Îµ',  '&equiv;' => 'â‰¡',  '&Eta;' => 'Î—',  '&eta;' => 'Î·',
    '&ETH;' => 'Ã�',  '&eth;' => 'Ã°',  '&Euml;' => 'Ã‹',  '&euml;' => 'Ã«',  '&euro;' => 'â‚¬',  '&exist;' => 'âˆƒ',  '&fnof;' => 'Æ’',  '&forall;' => 'âˆ€',
    '&frac12;' => 'Â½',  '&frac14;' => 'Â¼',  '&frac34;' => 'Â¾',  '&frasl;' => 'â�„',  '&Gamma;' => 'Î“',  '&gamma;' => 'Î³',  '&ge;' => 'â‰¥',  '&harr;' => 'â†”',
    '&hArr;' => 'â‡”',  '&hearts;' => 'â™¥',  '&hellip;' => 'â€¦',  '&Iacute;' => 'Ã�',  '&iacute;' => 'Ã­',  '&Icirc;' => 'ÃŽ',  '&icirc;' => 'Ã®',  '&iexcl;' => 'Â¡',
    '&Igrave;' => 'ÃŒ',  '&igrave;' => 'Ã¬',  '&image;' => 'â„‘',  '&infin;' => 'âˆž',  '&int;' => 'âˆ«',  '&Iota;' => 'Î™',  '&iota;' => 'Î¹',  '&iquest;' => 'Â¿',
    '&isin;' => 'âˆˆ',  '&Iuml;' => 'Ã�',  '&iuml;' => 'Ã¯',  '&Kappa;' => 'Îš',  '&kappa;' => 'Îº',  '&Lambda;' => 'Î›',  '&lambda;' => 'Î»',  '&lang;' => 'ã€ˆ',
    '&laquo;' => 'Â«',  '&larr;' => 'â†�',  '&lArr;' => 'â‡�',  '&lceil;' => 'âŒˆ',  '&ldquo;' => 'â€œ',  '&le;' => 'â‰¤',  '&lfloor;' => 'âŒŠ',  '&lowast;' => 'âˆ—',
    '&loz;' => 'â—Š',  '&lrm;' => 'â€Ž',  '&lsaquo;' => 'â€¹',  '&lsquo;' => 'â€˜',  '&macr;' => 'Â¯',  '&mdash;' => 'â€”',  '&micro;' => 'Âµ',  '&middot;' => 'Â·',
    '&minus;' => 'âˆ’',  '&Mu;' => 'Îœ',  '&mu;' => 'Î¼',  '&nabla;' => 'âˆ‡',  '&nbsp;' => 'Â ',  '&ndash;' => 'â€“',  '&ne;' => 'â‰ ',  '&ni;' => 'âˆ‹',
    '&not;' => 'Â¬',  '&notin;' => 'âˆ‰',  '&nsub;' => 'âŠ„',  '&Ntilde;' => 'Ã‘',  '&ntilde;' => 'Ã±',  '&Nu;' => 'Î�',  '&nu;' => 'Î½',  '&Oacute;' => 'Ã“',
    '&oacute;' => 'Ã³',  '&Ocirc;' => 'Ã”',  '&ocirc;' => 'Ã´',  '&OElig;' => 'Å’',  '&oelig;' => 'Å“',  '&Ograve;' => 'Ã’',  '&ograve;' => 'Ã²',  '&oline;' => 'â€¾',
    '&Omega;' => 'Î©',  '&omega;' => 'Ï‰',  '&Omicron;' => 'ÎŸ',  '&omicron;' => 'Î¿',  '&oplus;' => 'âŠ•',  '&or;' => 'âˆ¨',  '&ordf;' => 'Âª',  '&ordm;' => 'Âº',
    '&Oslash;' => 'Ã˜',  '&oslash;' => 'Ã¸',  '&Otilde;' => 'Ã•',  '&otilde;' => 'Ãµ',  '&otimes;' => 'âŠ—',  '&Ouml;' => 'Ã–',  '&ouml;' => 'Ã¶',  '&para;' => 'Â¶',
    '&part;' => 'âˆ‚',  '&permil;' => 'â€°',  '&perp;' => 'âŠ¥',  '&Phi;' => 'Î¦',  '&phi;' => 'Ï†',  '&Pi;' => 'Î ',  '&pi;' => 'Ï€',  '&piv;' => 'Ï–',
    '&plusmn;' => 'Â±',  '&pound;' => 'Â£',  '&prime;' => 'â€²',  '&Prime;' => 'â€³',  '&prod;' => 'âˆ�',  '&prop;' => 'âˆ�',  '&Psi;' => 'Î¨',  '&psi;' => 'Ïˆ',
    '&radic;' => 'âˆš',  '&rang;' => 'ã€‰',  '&raquo;' => 'Â»',  '&rarr;' => 'â†’',  '&rArr;' => 'â‡’',  '&rceil;' => 'âŒ‰',  '&rdquo;' => 'â€�',  '&real;' => 'â„œ',
    '&reg;' => 'Â®',  '&rfloor;' => 'âŒ‹',  '&Rho;' => 'Î¡',  '&rho;' => 'Ï�',  '&rlm;' => 'â€�',  '&rsaquo;' => 'â€º',  '&rsquo;' => 'â€™',  '&sbquo;' => 'â€š',
    '&Scaron;' => 'Å ',  '&scaron;' => 'Å¡',  '&sdot;' => 'â‹…',  '&sect;' => 'Â§',  '&shy;' => 'Â­',  '&Sigma;' => 'Î£',  '&sigma;' => 'Ïƒ',  '&sigmaf;' => 'Ï‚',
    '&sim;' => 'âˆ¼',  '&spades;' => 'â™ ',  '&sub;' => 'âŠ‚',  '&sube;' => 'âŠ†',  '&sum;' => 'âˆ‘',  '&sup1;' => 'Â¹',  '&sup2;' => 'Â²',  '&sup3;' => 'Â³',
    '&sup;' => 'âŠƒ',  '&supe;' => 'âŠ‡',  '&szlig;' => 'ÃŸ',  '&Tau;' => 'Î¤',  '&tau;' => 'Ï„',  '&there4;' => 'âˆ´',  '&Theta;' => 'Î˜',  '&theta;' => 'Î¸',
    '&thetasym;' => 'Ï‘',  '&thinsp;' => 'â€‰',  '&THORN;' => 'Ãž',  '&thorn;' => 'Ã¾',  '&tilde;' => 'Ëœ',  '&times;' => 'Ã—',  '&trade;' => 'â„¢',  '&Uacute;' => 'Ãš',
    '&uacute;' => 'Ãº',  '&uarr;' => 'â†‘',  '&uArr;' => 'â‡‘',  '&Ucirc;' => 'Ã›',  '&ucirc;' => 'Ã»',  '&Ugrave;' => 'Ã™',  '&ugrave;' => 'Ã¹',  '&uml;' => 'Â¨',
    '&upsih;' => 'Ï’',  '&Upsilon;' => 'Î¥',  '&upsilon;' => 'Ï…',  '&Uuml;' => 'Ãœ',  '&uuml;' => 'Ã¼',  '&weierp;' => 'â„˜',  '&Xi;' => 'Îž',  '&xi;' => 'Î¾',
    '&Yacute;' => 'Ã�',  '&yacute;' => 'Ã½',  '&yen;' => 'Â¥',  '&yuml;' => 'Ã¿',  '&Yuml;' => 'Å¸',  '&Zeta;' => 'Î–',  '&zeta;' => 'Î¶',  '&zwj;' => 'â€�',
    '&zwnj;' => 'â€Œ',  '&gt;' => '>',  '&lt;' => '<',  '&quot;' => '"',  '&apos;' => "'",
);

class UTF8 {
    public static function Length($string) {
        return strlen(utf8_decode($string));
    }

    public static function substr($str, $offset, $length = null,$word=false) {
        if( $length === null ) {
            $content= mb_substr($str, $offset);
        }else {
            if($word) {
                $l=mb_strpos($str,' ',$offset+$length,'UTF-8');
                if($l!==false)
                    $length=$l;
            }
            $content= trim(mb_substr($str, $offset, $length,'UTF-8'),'[\s,.]');
        }
        return $content;
    }
    public static  function ltrim($str,$charlist='') {
        if($charlist == '') return ltrim($str);

        //quote charlist for use in a characterclass
        $charlist = preg_replace('!([\\\\\\-\\]\\[/])!','\\\${1}',$charlist);

        return preg_replace('/^['.$charlist.']+/u','',$str);
    }

    public static function  rtrim($str,$charlist='') {
        if($charlist == '') return rtrim($str);

        //quote charlist for use in a characterclass
        $charlist = preg_replace('!([\\\\\\-\\]\\[/])!','\\\${1}',$charlist);

        return preg_replace('/['.$charlist.']+$/u','',$str);
    }

    public static function  trim($str,$charlist='') {
        if($charlist == '') return trim($str);

        return ltrim(rtrim($str));
    }

    public static function ToLower($string) {
        return mb_strtolower($string,'utf-8');
    }

    public static function ToUpper($string) {
        return mb_strtoupper($string,'utf-8');
    }
    public static  function eregi_replace($pattern,$replacement, $input) {
        return mb_eregi_replace($pattern, $replacement, $input) ;
    }

    public static function Pos($haystack, $needle, $offset=0) {
        return mb_strpos($haystack, $needle, $offset,'UTF-8');
    }
    public static function IPos($haystack, $needle, $offset=0) {
        return mb_stripos($haystack, $needle, $offset=0);
    }
    public static function PregPos($hs_pattern, $hs_subject, $hn_offset = 0) {
        $hs_foundstring = NULL;
        if (preg_match($hs_pattern, $hs_subject, $ha_matches, PREG_OFFSET_CAPTURE, $hn_offset)) {
            return $ha_matches[0][1];
        }
        else {
            return FALSE;
        }
    }
    public static function nosign($str) {
        $sign = array("à","á","ạ","ả","ã","â","ầ","ấ","ậ","ẩ","ẫ","ă",
            "ằ","ắ","ặ","ẳ","ẵ",
            "è","é","ẹ","ẻ","ẽ","ê","ề" ,"ế","ệ","ể","ễ",
            "ì","í","ị","ỉ","ĩ",
            "ò","ó","ọ","ỏ","õ","ô","ồ","ố","ộ","ổ","ỗ","ơ"
            ,"ờ","ớ","ợ","ở","ỡ",
            "ù","ú","ụ","ủ","ũ","ư","ừ","ứ","ự","ử","ữ",
            "ỳ","ý","ỵ","ỷ","ỹ","đ",
            "À","Á","Ạ","Ả","Ã","Â","Ầ","Ấ","Ậ","Ẩ","Ẫ","Ă"
            ,"Ằ","Ắ","Ặ","Ẳ","Ẵ",
            "È","É","Ẹ","Ẻ","Ẽ","Ê","Ề","Ế","Ệ","Ể","Ễ",
            "Ì","Í","Ị","Ỉ","Ĩ",
            "Ò","Ó","Ọ","Ỏ","Õ","Ô","Ồ","Ố","Ộ","Ổ","Ỗ","Ơ"
            ,"Ờ","Ớ","Ợ","Ở","Ỡ",
            "Ù","Ú","Ụ","Ủ","Ũ","Ư","Ừ","Ứ","Ự","Ử","Ữ",
            "Ỳ","Ý","Ỵ","Ỷ","Ỹ",
            "Đ","ê","ù","à");

        $unsign = array("a","a","a","a","a","a","a","a","a","a","a"
            ,"a","a","a","a","a","a",
            "e","e","e","e","e","e","e","e","e","e","e",
            "i","i","i","i","i",
            "o","o","o","o","o","o","o","o","o","o","o","o"
            ,"o","o","o","o","o",
            "u","u","u","u","u","u","u","u","u","u","u",
            "y","y","y","y","y",
            "d",
            "A","A","A","A","A","A","A","A","A","A","A","A"
            ,"A","A","A","A","A",
            "E","E","E","E","E","E","E","E","E","E","E",
            "I","I","I","I","I",
            "O","O","O","O","O","O","O","O","O","O","O","O"
            ,"O","O","O","O","O",
            "U","U","U","U","U","U","U","U","U","U","U",
            "Y","Y","Y","Y","Y",
            "D","e","u","a");
        return str_replace($sign, $unsign, $str);
    }
    public static function UCWords($str) {
        return mb_convert_case($str, MB_CASE_TITLE, "UTF-8");
    }

    public static function UCfirst($str) {
        switch ( self::length($str) ) {
            case 0:
                return '';
                break;
            case 1:
                return self::ToUpper($str);
                break;
            default:
                preg_match('/^(.{1})(.*)$/us', $str, $matches);
                return self::ToUpper($matches[1]).$matches[2];
                break;
        }
    }

    public static function UnHTML($text) {
        global $html_entities,$composite,$unicode;
        $text=str_replace('&amp;','&',$text);
        $text= preg_replace('/&(#x?)?([A-Za-z0-9]+);/e', 'self::_UnHTML("$1", "$2", "$0")', $text);
        return str_replace($composite,$unicode,$text);
    //return $text;
    }

    private  static function _UnHTML($prefix, $codepoint, $original) {
        global $html_entities;
        // Named entity
        if (!$prefix) {
        // A named entity not in the exclude list.
            if (isset($html_entities[$original])) {
                return $html_entities[$original];
            }
            else {
                return $original;
            }
        }
        // Hexadecimal numerical entity
        if ($prefix == '#x') {
            $codepoint = base_convert($codepoint, 16, 10);
        }
        // Decimal numerical entity (strip leading zeros to avoid PHP octal notation)
        else {
            $codepoint = preg_replace('/^0+/', '', $codepoint);
        }
        // Encode codepoint as UTF-8 bytes
        if ($codepoint < 0x80) {
            $str = chr($codepoint);
        }
        elseif ($codepoint < 0x800) {
            $str = chr(0xC0 | ($codepoint >> 6))
                . chr(0x80 | ($codepoint & 0x3F));
        }
        elseif ($codepoint < 0x10000) {
            $str = chr(0xE0 | ( $codepoint >> 12))
                . chr(0x80 | (($codepoint >> 6) & 0x3F))
                . chr(0x80 | ( $codepoint       & 0x3F));
        }
        elseif ($codepoint < 0x200000) {
            $str = chr(0xF0 | ( $codepoint >> 18))
                . chr(0x80 | (($codepoint >> 12) & 0x3F))
                . chr(0x80 | (($codepoint >> 6)  & 0x3F))
                . chr(0x80 | ( $codepoint        & 0x3F));
        }
        // Check for excluded characters
        if (isset($exclude[$str])) {
            return $original;
        }
        else {
            return $str;
        }
    }

    public static function Replace($content,$search,$replace, $offset=0) {
        $lengthOfSearchString  = self::Length($search);   // The length of the string to search
        $lengthOfReplaceString = self::Length($replace);  // The length of the string used to replace with
        $lengthOfTotalString   = self::Length($content); // The length of the haystack (the whole string)

        while (($offset        = self::Pos($content, $search, $offset)) !== false) {
        // co the push vao queue/array
            $content          = self::substr($content, 0, $offset).$replace.self::substr($content, $offset + $lengthOfSearchString, $lengthOfTotalString);
            $offset            = $offset + $lengthOfReplaceString; // New offset
            if ($offset        > $lengthOfTotalString)
                break;
        }
        return $content;
    }
    public static function SubStrCount($content,$needle) {
        return mb_substr_count($content,$needle);
    }
    public static function IsUpper($content,$offset=0) {
        global $Upper;
        $char=UTF8::substr($content,$offset,1);
        if(isset($Upper[$char]))
            return true;
        else return false;
    }
}
?>